package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.account.UserAccount;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

    private final UserAccountMapper userAccountMapper;

    @Autowired
    public UserAccountServiceImpl(UserAccountMapper userAccountMapper) {
        this.userAccountMapper = userAccountMapper;
    }

    /**
     * 账户初始化
     *
     * @param userId
     */
    @Override
    public void initAccount(Long userId) {
        // 初始化DO
        UserAccount userAccount = new UserAccount();
        userAccount.setUserId(userId);
        userAccount.setTotalAmount(new BigDecimal(0));
        userAccount.setAvailableAmount(new BigDecimal(0));
        userAccount.setTotalPayAmount(new BigDecimal(0));
        userAccount.setLockAmount(new BigDecimal(0));
        userAccount.setTotalIncomeAmount(new BigDecimal(0));
        if (!save(userAccount)) {
            throw new GuiguException(201, "初始化账户失败");
        }
    }
}
